# Maintainer: umarcor <unai.martinezcorral@ehu.eus>

_realname=scopehal-apps
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.0.0.r3329.gb2b889d8
pkgrel=1
pkgdesc="scopehal-apps: applications for libscopehal (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.ngscopeclient.org/"
msys2_repository_url="https://github.com/ngscopeclient/scopehal-apps"
license=('spdx:BSD-3-Clause')
groups=("${MINGW_PACKAGE_PREFIX}-eda")
depends=(
  "${MINGW_PACKAGE_PREFIX}-ffts"
  "${MINGW_PACKAGE_PREFIX}-glew"
  "${MINGW_PACKAGE_PREFIX}-glfw"
  "${MINGW_PACKAGE_PREFIX}-glm"
  "${MINGW_PACKAGE_PREFIX}-gtkmm3"
  "${MINGW_PACKAGE_PREFIX}-hidapi"
  "${MINGW_PACKAGE_PREFIX}-libsigc++"
  "${MINGW_PACKAGE_PREFIX}-vulkan-loader"
  "${MINGW_PACKAGE_PREFIX}-yaml-cpp"
  $([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-openmp")
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-shaderc"
  "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
  "git"
)
_commit='b2b889d87045c4e1848ab6859307519f7cfd875b'
source=("${_realname}::git+https://github.com/ngscopeclient/scopehal-apps.git#commit=${_commit}"
        "001-enable-avx-clang.patch::https://github.com/ngscopeclient/scopehal/pull/966.patch")
sha256sums=('2eeb71edb4d930e2152f797520c7a240746ea272d2304a51cf44d05d4b16963d'
            '440cc70e04eb40bf4e21c614553a214d5f8df39ebc9424b668e4ed0fb5fc9f15')

pkgver() {
  cd "${_realname}"
  printf "0.0.0.r%s.g%s" "$(git rev-list --count "${_commit}")" "$(git rev-parse --short=8 ${_commit})"
}

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}"
  git remote set-url origin https://github.com/ngscopeclient/scopehal-apps
  git submodule update --init --recursive

  cd "${srcdir}/${_realname}/lib"
  apply_patch_with_msg \
    001-enable-avx-clang.patch
}

build() {
  cd "${srcdir}"
  [ -d build-${MSYSTEM} ] && rm -rf build-${MSYSTEM}
  mkdir -p build-${MSYSTEM}
  cd build-${MSYSTEM}

  CMAKE="${MINGW_PREFIX}/bin/cmake.exe"

  CXXFLAGS+=" -Wno-format -Wno-error=maybe-uninitialized" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  $CMAKE \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DBUILD_TESTING=OFF \
    ../${_realname}

  $CMAKE --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
